package com.yanqu.road.server.manager.gourdchild;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.gourdchild.GourdChildBox;
import com.yanqu.road.entity.activity.gourdchild.GourdChildConfig;
import com.yanqu.road.entity.activity.gourdchild.GourdChildNotice;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.bussiness.activity.GourdChildBussiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.GourdChildProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

public class GourdChildActivity {

    private static Logger logger = LogManager.getLogger(GourdChildActivity.class.getName());

    private GourdChildConfig config;

    private GourdChildBox box;

    private List<GourdChildNotice> noticeList;


    public GourdChildActivity(ActivityInfo tmpActivityInfo) {
        // 读配置
        config = initConfig(tmpActivityInfo);

        box = GourdChildBussiness.getGourdChildBoxData(tmpActivityInfo.getActivityId());
        if(box == null){
            GourdChildBox tmpBox = new GourdChildBox();
            tmpBox.setActivityId(tmpActivityInfo.getActivityId());
            tmpBox.setCount(0);
            tmpBox.setInsertOption();
            box = tmpBox;
        }
        noticeList = GourdChildBussiness.getGourdChildNoticeList(tmpActivityInfo.getActivityId(), config.getMountainNoticeCount());
        // 排个序
        noticeList.sort(Comparator.comparingLong(GourdChildNotice::getTime));
    }

    private GourdChildConfig initConfig(ActivityInfo tmpActivityInfo) {
        GourdChildConfig tmpConfig = new GourdChildConfig();
        tmpConfig.setActivityInfo(tmpActivityInfo);
        int tmpActivityId = tmpActivityInfo.getActivityId();

        List<Integer> activityIdList = new ArrayList<>();
        activityIdList.add(tmpActivityInfo.getActivityId());

        // 其他配置
        Map<String, ActivityConfig> activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(tmpActivityId);

        //盲盒配置
        tmpConfig.setMountainConsume(activityConfigMap.get(eActivityConfigType.GOURD_CHILD_MOUNTAIN_CONSUME.getName()).getValue());
        tmpConfig.setMountainLuckyParam(StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.GOURD_CHILD_MOUNTAIN_LUCKY_PARAM.getName()).getValue(), ";"));
        tmpConfig.setHideBoxCount(tmpConfig.getMountainLuckyParam().get(1) * tmpActivityInfo.getServerIdList().size());


        tmpConfig.setMountainNoticeCount(activityConfigMap.get(eActivityConfigType.GOURD_CHILD_MOUNTAIN_NOTICE_COUNT.getName()).getIntValue());
        return tmpConfig;
    }

    public void save() {
        if (box.isInsertOption()) {
            GourdChildBussiness.addGourdChildBoxData(box);
        } else if (box.isUpdateOption()) {
            GourdChildBussiness.updateGourdChildBoxData(box);
        }

        int size = noticeList.size();
        for (int i = 0; i < size; i++) {
            GourdChildNotice notice = noticeList.get(i);
            if (notice.isInsertOption()) {
                GourdChildBussiness.addGourdChildNotice(notice);
            }
        }
    }

    /**
     * 抽低价隐藏盲盒
     */
    public boolean drawHideBox() {
        synchronized (box) {
            if (box.getCount() < config.getHideBoxCount()) {
                box.setCount(box.getCount() + 1);
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     * 添加公告
     */
    public void uploadNotice(long userId, int rewardId, UserBaseInfo userBaseInfo, int type) {
        GourdChildNotice notice = new GourdChildNotice();
        notice.setActivityId(config.getActivityId());
        notice.setUserId(userId);
        notice.setUserBaseInfo(userBaseInfo);
        notice.setRewardId(rewardId);
        notice.setTime(System.currentTimeMillis());
        notice.setType(type);
        notice.setInsertOption();

        synchronized (noticeList) {
            noticeList.add(notice);
        }

        // 同步到区服
        GourdChildProto.SyncGourdChildNoticeRespMsg.Builder builder = GourdChildProto.SyncGourdChildNoticeRespMsg.newBuilder();
        builder.setNotice(CrossGourdChildMgr.parseNoticeMsg(notice));
        YanQuMessage yanQuMessage = YanQuMessageUtils.buildMessage(GameProtocol.S_GOURD_CHILD_SYNC_NOTICE, builder);
        for (Long serverId : config.getActivityInfo().getServerIdList()) {
            MessageHelper.sendPacket(serverId, 0, yanQuMessage);
        }
    }


    /**
     * 重新加载配置
     */
    public void reloadConfig() {
        int activityId = config.getActivityId();
        ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
        config = initConfig(activityInfo);
    }

    /**
     * 热更
     */
    public void repair() {

    }

    /**
     * 获取公告
     */
    public List<GourdChildNotice> getNoticeData() {
        int maxCount = config.getMountainNoticeCount();
        List<GourdChildNotice> tmpList = new ArrayList<>(noticeList);
        if (tmpList.size() > maxCount) {
            List<GourdChildNotice> result = new ArrayList<>();
            int size = tmpList.size();
            for (int i = size - 1; i >= size - maxCount ; i--) {
                result.add(tmpList.get(i));
            }
            return tmpList.subList(tmpList.size() - maxCount, tmpList.size());
        } else {
            return tmpList;
        }
    }

}
